package com.dailyblue.java.spring.boot.redis.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dailyblue.java.spring.boot.redis.bean.Emp;
import com.dailyblue.java.spring.boot.redis.mapper.EmpMapper;
import com.dailyblue.java.spring.boot.redis.service.EmpService;
import com.dailyblue.java.spring.boot.redis.util.JsonResult;
import com.dailyblue.java.spring.boot.redis.util.ResultTool;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service
@Slf4j
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {
    @Resource
    private EmpMapper empMapper;

    @Cacheable(cacheNames = "find", key = "0")
    public JsonResult find() {
        return ResultTool.success(list());
    }

    @Cacheable(cacheNames = "find", key = "#page")
    public JsonResult find(int page) {
        PageHelper.startPage(page, 5);
        return ResultTool.success(list());
    }

    @CachePut(cacheNames = "find", key = "0")
    @Transactional
    public JsonResult delete(int id) {
        log.info("id:{}", id);
        empMapper.deleteById(id);
        return ResultTool.success(list());
    }

}
